#include "fxcc/core/cmdUtils.h"

bool cmdUtils::CreateCubeDDSFromImage(const std::vector<std::string>& sources, const std::string& target, glm::ivec2 sz ,bool mipmap)
{
	assert(sources.size() == 6);

	fxcc::Base::CreateParentDirs(target);

	kainjow::mustache::mustache m1 = R"(texassemble cube -y -w {{{width}}} -h {{{height}}} -o {{{target}}} {{#images}} {{{.}}} {{/images}})";
	kainjow::mustache::data d1;

	d1.set("target", target);
	d1.set("width", std::to_string(sz.x));
	d1.set("height", std::to_string(sz.x));


	kainjow::mustache::list l1;
	for (int i = 0; i < 6; i++)
	{
		l1.push_back(sources[i]);
	}

	d1.set("images", l1);
	std::string cmd = m1.render(d1);

	return sysx::System::ExecuteCmd(cmd);
}

bool cmdUtils::CreateArrayDDSfromImage(const std::vector<std::string>& source, const std::string& target, glm::ivec2 sz)
{
	fxcc::Base::CreateParentDirs(target);

	kainjow::mustache::mustache m1 = R"(texassemble array -y -w {{{width}}} -h {{{height}}} -o {{{target}}} {{#images}} {{{.}}} {{/images}})";
	kainjow::mustache::data d1;

	d1.set("target", target);
	d1.set("width", std::to_string(sz.x));
	d1.set("height", std::to_string(sz.x));

	kainjow::mustache::list l1;
	for (const auto& s : source)
	{
		l1.push_back(s);
	}

	d1.set("images", l1);
	std::string cmd = m1.render(d1);

	return sysx::System::ExecuteCmd(cmd);
}

bool cmdUtils::Create3dDDSFromImage(const std::vector<std::string>& source, const std::string& target, glm::ivec2 sz)
{
	fxcc::Base::CreateParentDirs(target);

	kainjow::mustache::mustache m1 = R"(texassemble  volume -w {{{width}}} -h {{{height}}} -y   -o {{{target}}} {{#images}} {{{.}}} {{/images}})";
	kainjow::mustache::data d1;

	d1.set("target", target);
	d1.set("width", std::to_string(sz.x));
	d1.set("height", std::to_string(sz.x));

	kainjow::mustache::list l1;
	for (const auto& s : source)
	{
		l1.push_back(s);
	}

	d1.set("images", l1);
	std::string cmd = m1.render(d1);

	return sysx::System::ExecuteCmd(cmd);
}

bool cmdUtils::CreateDDSFromImage(const std::string& source, const std::string& target, glm::ivec2 sz)
{
	fxcc::Base::CreateParentDirs(target);

	fs::path _p(target);
	fs::path _dir = _p.parent_path();

	fs::path _sourcePath(source);

	std::string filenameNoSuffix = fxcc::Base::GetNoSuffixName(_sourcePath.filename().string());
	fs::path newPath = _dir / (filenameNoSuffix + ".dds");;

	bool flag = TranslateSTBtoDDSDir(source, _dir.string(), sz);

	if (fs::exists(newPath))
	{
		fs::rename(newPath, target);
	}
	
	return flag;
}

bool cmdUtils::TranslateSTBtoDDSDir(const std::string& source, const std::string& targetDir, glm::ivec2 sz)
{
	kainjow::mustache::mustache m1 = std::string(R"(texconv -y -w {{{width}}} -h {{{height}}} -f BC3_UNORM -o  "{{{targetDir}}}" "{{{source}}}")");

	kainjow::mustache::data d1;

	d1.set("source", source);
	d1.set("targetDir", targetDir);
	d1.set("width", std::to_string(sz.x));
	d1.set("height", std::to_string(sz.x));

	return sysx::System::ExecuteCmd(m1.render(d1));
}

bool cmdUtils::TransAudioFileToOgg(const std::string& source, const std::string& target)
{
	if (!fs::exists(source))
	{
		return false;
	}

	fxcc::Base::CreateParentDirs(target);

	kainjow::mustache::mustache m1 = std::string(
		R"(ffmpeg -y  -i "{{{source}}}" -c:a libvorbis "{{{targetPath}}}")"
	);
	kainjow::mustache::data d1;

	d1.set("source", source);
	d1.set("targetPath", target);
	return sysx::System::ExecuteCmd(m1.render(d1));

}